package com.jungle.test.tool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Jungle
 * @create 2023-09-20 7:08
 */
public class DateUtil {
    private static final ThreadLocal<SimpleDateFormat> threadLocal =ThreadLocal.withInitial(()->new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    public static Date parse(String dateStr) {
        //每次都new对象可以解决问题，但是比较浪费空间
        //SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = threadLocal.get().parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 解决多线程问题
     */
    //private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    //
    //public static LocalDateTime parse(String dateStr) {
    //    return LocalDateTime.parse(dateStr, formatter);
    //}
}
